package com.zzh.controller;

import com.github.pagehelper.PageInfo;
import com.zzh.domain.ResponseResult;
import com.zzh.domain.Role;
import com.zzh.domain.User;
import com.zzh.domain.UserVo;
import com.zzh.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/findAllUserByPage")
    public ResponseResult findAllUserByPage(@RequestBody UserVo userVo) {
        PageInfo<User> pageInfo = userService.findAllUserByPage(userVo);
        List<User> list = pageInfo.getList();
        System.out.println("list = " + list);
        return new ResponseResult(true, 200, "成功响应", pageInfo);
    }

    @RequestMapping("/updateUserStatus")
    public ResponseResult updateUserStatus(@RequestParam int id, @RequestParam String status) {
        //ENABLE能登录，DISABLE不能登录
        if ("ENABLE".equalsIgnoreCase(status)) {
            status = "DISABLE";
        } else {
            status = "ENABLE";
        }
        userService.updateUserStatus(id, status);
        return new ResponseResult(true, 200, "成功响应", status);
    }

    @RequestMapping("/login")
    public ResponseResult login(User user, HttpServletRequest request) {
        User login = userService.login(user);
        ResponseResult result = null;
        if (login != null) {
            Map<String, Object> map = new HashMap<>();
            String uuid = UUID.randomUUID().toString();
            map.put("access_token", uuid);
            map.put("user_id", login.getId());
            HttpSession session = request.getSession();
            session.setAttribute("user_id", login.getId());
            session.setAttribute("access_token", uuid);
            result = new ResponseResult(true, 1, "响应成功", map);
        } else {
            result = new ResponseResult(false, 1, "用户名或者密码错误", null);
        }
        return result;
    }

    @RequestMapping("/findUserRoleById")
    public ResponseResult findUserRoleById(int id) {
        List<Role> roles = userService.findUserRoleById(id);
        return new ResponseResult(true, 200, "分配角色成功", roles);
    }

    @RequestMapping("/userContextRole")
    public ResponseResult userContextRole(@RequestBody UserVo userVo) {
        userService.userContextRole(userVo);
        return new ResponseResult(true, 200, "成功响应", null);
    }

    /**
     * 获取用户权限
     */
    @RequestMapping("/getUserPermissions")
    public ResponseResult getUserPermissions(HttpServletRequest request) {
        //获取请求头的token
        String token = request.getHeader("Authorization");
        //获取session的access_token
        HttpSession session = request.getSession();
        String access_token = (String) session.getAttribute("access_token");
        if (token.equals(access_token)) {
            int userId = (int) session.getAttribute("user_id");
            ResponseResult result = userService.getUserPermissions(userId);
            return result;
        }else {
            ResponseResult result = new ResponseResult(false, 400, "获取失败", "");
            return result;
        }
    }
}
